<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CartController
 *
 * @author Lawrence
 */
Yii::import('application.extensions.Util.*');
Yii::import('application.modules.admin.models.Orders');
Yii::import('application.modules.admin.models.OrderDetail');

class CartController extends ControllerBase {

    public $cart;

    public function init() {
        $this->cart = Yii::app()->shoppingCart;
    }

    public function actionIndex() {
        $model = new Orders('insert');
        if (!Yii::app()->user->isGuest) {
            $model->attributes = array(
//                'fullname' => Yii::app()->user->getState('Fullname'),
//                'address' => Yii::app()->user->getState('Address'),
                'phone' => Yii::app()->user->getState('Phone'),
                'email' => Yii::app()->user->getState('Email'),
            );
        }

        if (isset($_POST['Quantity'])) {
            $quantities = $_POST['Quantity'];
            foreach ($quantities as $key => $value) :
                $item = $this->cart->itemAt($key);
                $this->cart->update($item, $value);
            endforeach;
            $this->refresh();
        }

        if (isset($_POST['Orders'])) {
            $model->attributes = $_POST['Orders'];
            $model->validate();
            if (!$model->hasErrors()) {
                $account = array(
                    'fullname' => $model->fullname,
                    'address' => $model->address,
                    'phone' => $model->phone,
                    'email' => $model->email,
                );
//                $shipping = array(
//                    'f' => !empty($model->f) ? $model->f : $model->fullname,
//                    'a' => !empty($model->a) ? $model->a : $model->address,
//                    'p' => !empty($model->p) ? $model->p : $model->phone,
//                    'e' => !empty($model->e) ? $model->e : $model->email,
//                    'time' => $model->time,
//                );
                $model->account = serialize($account);
                $model->shipping = '';
                $model->total = $this->cart->getCost();
                $model->created = time();

                if ($model->save()) {
                    $carts = $this->cart->getPositions();

                    foreach ($carts as $node) :
                        $orderDetails = new OrderDetail();
                        $product = array(
                            'name' => $node->name,
                            'price' => $node->getPrice(),
                            'quantity' => $node->getQuantity(),
                            'sumPrice' => $node->getSumPrice()
                        );
                        $orderDetails->attributes = array(
                            'order_id' => $model->id,
                            'product' => serialize($product),
                        );
                        if (!$orderDetails->save()) {
                            echo $orderDetails->getError('product');
                            echo $orderDetails->getError('order_id');
                            exit;
                        }
                    endforeach;

                    $this->cart->clear();
                    Yii::app()->user->setFlash('message', 'successfull');
                    $this->refresh();
                }
            }
        }

        $this->render('index', array('model' => $model));
    }

    public function actionAdd($id) {
        $product = Product::model()->findByPk($id);
        if ($product !== null) {
            $this->cart->put($product, 1);
            $this->redirect(array('index'));
        }
    }

    public function actionDelete($id) {
        $this->cart->remove($id);
        $this->redirect(array('//cart/index'));
    }

    public function actionClear() {
        $this->cart->clear();
    }

//    public function beforeAction() {
//        if (Yii::app()->user->isGuest) {
//            $this->render('invalid_page');
//            return false;
//        }
//
//        $userid = Yii::app()->user->id;
//        $role = Yii::app()->getModule('user')->getRole();
//        $actionName = Yii::app()->controller->getAction()->id;
//
//        $role = Util::isAllowProcess($userid, $role, 'Brand', $actionName);
//
//        if ($role == 'customer') {
//            if (count($role))
//                return true;
//            else
//                $this->render('role_limit');
//        }
//
//        return true;
//    }

}

?>
